<!DOCTYPE HTML>
<html lang="zh-CN">


<head>
    <meta charset="utf-8">
    <meta name="keywords" content="做题心得-方法论-十大排序(2), 胡言乱语">
    <meta name="description" content="做题心得(剑指offer + Hot 100 +  代码随想录 + Code Top 频率&amp;gt;&amp;#x3D;10)前言:
本心得是本人在刷剑指offer + Hot 100 +  代码随想录 + Code Top 频率&amp;gt;&amp;#x3D;">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
    <meta name="renderer" content="webkit|ie-stand|ie-comp">
    <meta name="mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
    <meta name="referrer" content="no-referrer-when-downgrade">
    <!-- Global site tag (gtag.js) - Google Analytics -->


    

    <title>做题心得-方法论-十大排序(2) | 胡言乱语</title>
    <link rel="icon" type="image/x-icon, image/vnd.microsoft.icon" href="/myblog/favicon.ico">

    <link rel="stylesheet" type="text/css" href="/myblog/libs/awesome/css/all.min.css">
    <link rel="stylesheet" type="text/css" href="/myblog/libs/materialize/materialize.min.css">
    <link rel="stylesheet" type="text/css" href="/myblog/libs/aos/aos.css">
    <link rel="stylesheet" type="text/css" href="/myblog/libs/animate/animate.min.css">
    <link rel="stylesheet" type="text/css" href="/myblog/libs/lightGallery/css/lightgallery.min.css">
    <link rel="stylesheet" type="text/css" href="/myblog/css/matery.css">
    <link rel="stylesheet" type="text/css" href="/myblog/css/my.css">

    <script src="/myblog/libs/jquery/jquery-3.6.0.min.js"></script>

<meta name="generator" content="Hexo 6.2.0"></head>


<body>
    <header class="navbar-fixed">
    <nav id="headNav" class="bg-color nav-transparent">
        <div id="navContainer" class="nav-wrapper container">
            <div class="brand-logo">
                <a href="/myblog/" class="waves-effect waves-light">
                    
                    <img src="/myblog/medias/logo.png" class="logo-img" alt="LOGO">
                    
                    <span class="logo-span">胡言乱语</span>
                </a>
            </div>
            

<a href="#" data-target="mobile-nav" class="sidenav-trigger button-collapse"><i class="fas fa-bars"></i></a>
<ul class="right nav-menu">
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/myblog/" class="waves-effect waves-light">
      
      <i class="fas fa-home" style="zoom: 0.6;"></i>
      
      <span>首页</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/myblog/tags" class="waves-effect waves-light">
      
      <i class="fas fa-tags" style="zoom: 0.6;"></i>
      
      <span>标签</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/myblog/categories" class="waves-effect waves-light">
      
      <i class="fas fa-bookmark" style="zoom: 0.6;"></i>
      
      <span>分类</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/myblog/archives" class="waves-effect waves-light">
      
      <i class="fas fa-archive" style="zoom: 0.6;"></i>
      
      <span>归档</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/myblog/about" class="waves-effect waves-light">
      
      <i class="fas fa-user-circle" style="zoom: 0.6;"></i>
      
      <span>关于</span>
    </a>
    
  </li>
  
  <li>
    <a href="#searchModal" class="modal-trigger waves-effect waves-light">
      <i id="searchIcon" class="fas fa-search" title="搜索" style="zoom: 0.85;"></i>
    </a>
  </li>
</ul>


<div id="mobile-nav" class="side-nav sidenav">

    <div class="mobile-head bg-color">
        
        <img src="/myblog/medias/logo.png" class="logo-img circle responsive-img">
        
        <div class="logo-name">胡言乱语</div>
        <div class="logo-desc">
            
            Never really desperate, only the lost of the soul.
            
        </div>
    </div>

    <ul class="menu-list mobile-menu-list">
        
        <li class="m-nav-item">
	  
		<a href="/myblog/" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-home"></i>
			
			首页
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/myblog/tags" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-tags"></i>
			
			标签
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/myblog/categories" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-bookmark"></i>
			
			分类
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/myblog/archives" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-archive"></i>
			
			归档
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/myblog/about" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-user-circle"></i>
			
			关于
		</a>
          
        </li>
        
        
    </ul>
</div>


        </div>

        
    </nav>

</header>

    



<div class="bg-cover pd-header post-cover" style="background-image: url('/myblog/medias/featureimages/6.jpg')">
    <div class="container" style="right: 0px;left: 0px;">
        <div class="row">
            <div class="col s12 m12 l12">
                <div class="brand">
                    <h1 class="description center-align post-title">做题心得-方法论-十大排序(2)</h1>
                </div>
            </div>
        </div>
    </div>
</div>




<main class="post-container content">

    
    <link rel="stylesheet" href="/myblog/libs/tocbot/tocbot.css">
<style>
    #articleContent h1::before,
    #articleContent h2::before,
    #articleContent h3::before,
    #articleContent h4::before,
    #articleContent h5::before,
    #articleContent h6::before {
        display: block;
        content: " ";
        height: 100px;
        margin-top: -100px;
        visibility: hidden;
    }

    #articleContent :focus {
        outline: none;
    }

    .toc-fixed {
        position: fixed;
        top: 64px;
    }

    .toc-widget {
        width: 345px;
        padding-left: 20px;
    }

    .toc-widget .toc-title {
        padding: 35px 0 15px 17px;
        font-size: 1.5rem;
        font-weight: bold;
        line-height: 1.5rem;
    }

    .toc-widget ol {
        padding: 0;
        list-style: none;
    }

    #toc-content {
        padding-bottom: 30px;
        overflow: auto;
    }

    #toc-content ol {
        padding-left: 10px;
    }

    #toc-content ol li {
        padding-left: 10px;
    }

    #toc-content .toc-link:hover {
        color: #42b983;
        font-weight: 700;
        text-decoration: underline;
    }

    #toc-content .toc-link::before {
        background-color: transparent;
        max-height: 25px;

        position: absolute;
        right: 23.5vw;
        display: block;
    }

    #toc-content .is-active-link {
        color: #42b983;
    }

    #floating-toc-btn {
        position: fixed;
        right: 15px;
        bottom: 76px;
        padding-top: 15px;
        margin-bottom: 0;
        z-index: 998;
    }

    #floating-toc-btn .btn-floating {
        width: 48px;
        height: 48px;
    }

    #floating-toc-btn .btn-floating i {
        line-height: 48px;
        font-size: 1.4rem;
    }
</style>
<div class="row">
    <div id="main-content" class="col s12 m12 l9">
        <!-- 文章内容详情 -->
<div id="artDetail">
    <div class="card">
        <div class="card-content article-info">
            <div class="row tag-cate">
                <div class="col s7">
                    
                    <div class="article-tag">
                        
                            <a href="/myblog/tags/%E7%AE%97%E6%B3%95/">
                                <span class="chip bg-color">算法</span>
                            </a>
                        
                            <a href="/myblog/tags/%E6%8E%92%E5%BA%8F%E7%AE%97%E6%B3%95/">
                                <span class="chip bg-color">排序算法</span>
                            </a>
                        
                    </div>
                    
                </div>
                <div class="col s5 right-align">
                    
                    <div class="post-cate">
                        <i class="fas fa-bookmark fa-fw icon-category"></i>
                        
                            <a href="/myblog/categories/%E5%81%9A%E9%A2%98%E5%BF%83%E5%BE%97%E4%B8%93%E6%A0%8F/" class="post-category">
                                做题心得专栏
                            </a>
                        
                    </div>
                    
                </div>
            </div>

            <div class="post-info">
                
                <div class="post-date info-break-policy">
                    <i class="far fa-calendar-minus fa-fw"></i>发布日期:&nbsp;&nbsp;
                    2022-06-04
                </div>
                

                
                <div class="post-date info-break-policy">
                    <i class="far fa-calendar-check fa-fw"></i>更新日期:&nbsp;&nbsp;
                    2022-06-04
                </div>
                

                
                <div class="info-break-policy">
                    <i class="far fa-file-word fa-fw"></i>文章字数:&nbsp;&nbsp;
                    6k
                </div>
                

                
                <div class="info-break-policy">
                    <i class="far fa-clock fa-fw"></i>阅读时长:&nbsp;&nbsp;
                    24 分
                </div>
                

                
                    <div id="busuanzi_container_page_pv" class="info-break-policy">
                        <i class="far fa-eye fa-fw"></i>阅读次数:&nbsp;&nbsp;
                        <span id="busuanzi_value_page_pv"></span>
                    </div>
				
            </div>
        </div>
        <hr class="clearfix">

        
        <!-- 是否加载使用自带的 prismjs. -->
        <link rel="stylesheet" href="/myblog/libs/prism/prism.min.css">
        

        
        <!-- 代码块折行 -->
        <style type="text/css">
            code[class*="language-"], pre[class*="language-"] { white-space: pre-wrap !important; }
        </style>
        

        <div class="card-content article-card-content">
            <div id="articleContent">
                <h1 id="做题心得-剑指offer-Hot-100-代码随想录-Code-Top-频率-gt-x3D-10"><a href="#做题心得-剑指offer-Hot-100-代码随想录-Code-Top-频率-gt-x3D-10" class="headerlink" title="做题心得(剑指offer + Hot 100 +  代码随想录 + Code Top 频率&gt;&#x3D;10)"></a>做题心得(剑指offer + Hot 100 +  代码随想录 + Code Top 频率&gt;&#x3D;10)</h1><h2 id="前言"><a href="#前言" class="headerlink" title="前言:"></a>前言:</h2><ul>
<li><p>本心得是本人在刷剑指offer + Hot 100 +  代码随想录 + Code Top 频率&gt;&#x3D;10的过程中, 当做到一种题有点可以总结的想法时就写下的(例如:回溯的总结),]</p>
<p>所以可能会出现前后重复甚至是矛盾的观点, 这也是代表着本人的一些成长吧hh</p>
</li>
<li><p>因为过多, 所以重要的会在这条前面用*来表示其重要性</p>
</li>
</ul>
<h2 id="做题杂谈-通用的一些思想或后面的方法论的选用"><a href="#做题杂谈-通用的一些思想或后面的方法论的选用" class="headerlink" title="做题杂谈:通用的一些思想或后面的方法论的选用"></a>做题杂谈:通用的一些思想或后面的方法论的选用</h2><p>​                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          </p>
<h2 id="方法论"><a href="#方法论" class="headerlink" title="方法论:"></a>方法论:</h2><ul>
<li><p>这里是本人遇到过的算法类型的总结, 有以下算法类型: 涉及: 数据结构相关操作(xx操作) &amp;&amp; 算法模板与解析</p>
<p>(以下有些算法, 是笔者为了方便记忆, 基于其特征来自主命名的, 可能有更官方的名字吧hh)</p>
<ul>
<li>十大排序(重点: 快排,归并,堆排)</li>
<li>数组操作(说的是关于数组本身处理的一些题目)</li>
<li>链表操作(说的是关于链表这个数据结构本身处理的一些题目)</li>
<li>字符串操作</li>
<li>哈希表法</li>
<li>二分查找法</li>
<li>双指针(左右双指针、快慢双指针、滑动窗口）</li>
<li>回溯法(基础回溯模板, 三大重复问题, 各种回溯题型: 排列、组合、分割、子集）</li>
<li>DP动态规划(基础DP思想, 01背包问题, 完全背包问题, 股票问题(多状态DP), 逻辑二维DP(子序列问题、回文问题) ）</li>
<li>位运算</li>
<li>二叉树（DFS、BFS、二维DFS）</li>
<li>岛屿问题（二维DFS的应用）</li>
</ul>
</li>
</ul>
<h3 id="十大排序-重点-快排-归并-堆排"><a href="#十大排序-重点-快排-归并-堆排" class="headerlink" title="十大排序(重点: 快排,归并,堆排)"></a>十大排序(重点: 快排,归并,堆排)</h3><ul>
<li>十大排序总结比较: (下面只会介绍一些算法题会用到的, 重点其实就是: 快排、归并、堆排)</li>
</ul>
<p><img src="/myblog/%5CmdImage%5Cimage-20220602162740717.png" alt="image-20220602162740717"></p>
<p>​		</p>
<ul>
<li><p>选择排序:</p>
<ul>
<li><p>时间复杂度: O(n^2)</p>
</li>
<li><p>空间复杂度: O(1)</p>
</li>
<li><p>思想: 每次选择当前位置之后最小的一个数, 放到当前位置</p>
</li>
<li><p>代码实现:</p>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> nums<span class="token punctuation">.</span>length<span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>         
    <span class="token keyword">int</span> minIndex <span class="token operator">=</span> i<span class="token punctuation">;</span>                           
    <span class="token comment">// 向后寻找最小位置                         </span>
    <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> j <span class="token operator">=</span> i <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">;</span> j <span class="token operator">&lt;</span> nums<span class="token punctuation">.</span>length<span class="token punctuation">;</span> j<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span> 
        <span class="token keyword">if</span><span class="token punctuation">(</span>nums<span class="token punctuation">[</span>minIndex<span class="token punctuation">]</span> <span class="token operator">></span> nums<span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>          
            minIndex <span class="token operator">=</span> j<span class="token punctuation">;</span>                       
        <span class="token punctuation">&#125;</span>                                       
    <span class="token punctuation">&#125;</span>                                           
    <span class="token comment">// 交换                                     </span>
    <span class="token keyword">if</span><span class="token punctuation">(</span>minIndex <span class="token operator">!=</span> i<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>                         
        <span class="token keyword">int</span> temp <span class="token operator">=</span> nums<span class="token punctuation">[</span>minIndex<span class="token punctuation">]</span><span class="token punctuation">;</span>              
        nums<span class="token punctuation">[</span>minIndex<span class="token punctuation">]</span> <span class="token operator">=</span> nums<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">;</span>               
        nums<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">=</span> temp<span class="token punctuation">;</span>                         
    <span class="token punctuation">&#125;</span>                                           
<span class="token punctuation">&#125;</span>                                                   <span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></li>
</ul>
</li>
<li><p>插入排序:</p>
<ul>
<li><p>时间复杂度: O(n^2)</p>
</li>
<li><p>空间复杂度: O(1)</p>
</li>
<li><p>类型: 直接插入排序:</p>
</li>
<li><p>思想:<br>认为[0 : i-1]是已经排序好的序列, 则对于新加入的[i],<br> 依次向前比较大小并交换, 直到大小关系符合要求, 停止比较<br> 这样对于每一个新加入的[i]都可以找到其在序列中的正确位置</p>
</li>
<li><p>代码实现:</p>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> nums<span class="token punctuation">.</span>length<span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>                          
    <span class="token keyword">int</span> curIndex <span class="token operator">=</span> i<span class="token punctuation">;</span>                                            
    <span class="token comment">// 和前一个比较大小,往前不断确定自己的位置                   </span>
    <span class="token keyword">while</span><span class="token punctuation">(</span>curIndex <span class="token operator">>=</span> <span class="token number">1</span> <span class="token operator">&amp;&amp;</span> nums<span class="token punctuation">[</span>curIndex<span class="token punctuation">]</span> <span class="token operator">&lt;</span> nums<span class="token punctuation">[</span>curIndex <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
        <span class="token keyword">int</span> tmp <span class="token operator">=</span> nums<span class="token punctuation">[</span>curIndex<span class="token punctuation">]</span><span class="token punctuation">;</span>                                
        nums<span class="token punctuation">[</span>curIndex<span class="token punctuation">]</span> <span class="token operator">=</span> nums<span class="token punctuation">[</span>curIndex <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">;</span>                     
        nums<span class="token punctuation">[</span>curIndex <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">]</span> <span class="token operator">=</span> tmp<span class="token punctuation">;</span>                                
        curIndex<span class="token operator">--</span><span class="token punctuation">;</span>                                              
    <span class="token punctuation">&#125;</span>                                                            
<span class="token punctuation">&#125;</span>      <span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></li>
</ul>
</li>
<li><p>冒泡排序:</p>
<ul>
<li><p>时间复杂度: O(n^2)</p>
</li>
<li><p>空间复杂度: O(1)</p>
</li>
<li><p>思想:</p>
<p>​	循环n-1次, 对于每一次循环, 都从<a href="%E4%B9%8B%E5%90%8E%E6%98%AF%E5%B7%B2%E7%BB%8F%E7%A1%AE%E5%AE%9A%E5%A5%BD%E4%BD%8D%E7%BD%AE%E7%9A%84">0,nums.length - 1 - i</a>中,<br>​     相邻的逐个比较, 那么符合最大值&#x2F;最小值的就会跑到最后, 就跟水泡一个个网上冒一样,所以叫冒泡排序</p>
</li>
<li><p>代码实现:</p>
<pre class="line-numbers language-java" data-language="java"><code class="language-java">基础版本<span class="token punctuation">(</span> 无论如何时间复杂度都是<span class="token class-name">O</span><span class="token punctuation">(</span>n<span class="token operator">^</span><span class="token number">2</span><span class="token punctuation">)</span> <span class="token punctuation">)</span>                                                        
    <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> nums<span class="token punctuation">.</span>length <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>                                                 
        <span class="token comment">// 在未排序区间里, 左右相邻比较交换, 从而使得最大的跑到最后去                           </span>
        <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> j <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> j <span class="token operator">&lt;</span> nums<span class="token punctuation">.</span>length <span class="token operator">-</span> <span class="token number">1</span> <span class="token operator">-</span> i<span class="token punctuation">;</span> j<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>                                         
            <span class="token keyword">if</span><span class="token punctuation">(</span>nums<span class="token punctuation">[</span>j<span class="token punctuation">]</span> <span class="token operator">></span> nums<span class="token punctuation">[</span>j <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>                                                         
                <span class="token keyword">int</span> temp <span class="token operator">=</span> nums<span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">;</span>                                                             
                nums<span class="token punctuation">[</span>j<span class="token punctuation">]</span> <span class="token operator">=</span> nums<span class="token punctuation">[</span>j<span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">;</span>                                                            
                nums<span class="token punctuation">[</span>j <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">]</span> <span class="token operator">=</span> temp<span class="token punctuation">;</span>                                                             
            <span class="token punctuation">&#125;</span>                                                                                   
        <span class="token punctuation">&#125;</span>                                                                                       
    <span class="token punctuation">&#125;</span>                                                                                           
优化版本<span class="token punctuation">(</span>最优时间复杂度是<span class="token class-name">O</span><span class="token punctuation">(</span><span class="token class-name">N</span><span class="token punctuation">)</span><span class="token punctuation">,</span> 因为如果后面已经排好序了<span class="token punctuation">,</span> 第一轮即可判断之后无需执行<span class="token punctuation">)</span>            
    <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> nums<span class="token punctuation">.</span>length <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>                                                 
        <span class="token keyword">boolean</span> flag <span class="token operator">=</span> <span class="token boolean">false</span><span class="token punctuation">;</span>                                                                   
        <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> j <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> j <span class="token operator">&lt;</span> nums<span class="token punctuation">.</span>length <span class="token operator">-</span> <span class="token number">1</span> <span class="token operator">-</span> i<span class="token punctuation">;</span> j<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>                                         
            <span class="token keyword">if</span><span class="token punctuation">(</span>nums<span class="token punctuation">[</span>j<span class="token punctuation">]</span> <span class="token operator">></span> nums<span class="token punctuation">[</span>j <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>                                                         
                <span class="token keyword">int</span> temp <span class="token operator">=</span> nums<span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">;</span>                                                             
                nums<span class="token punctuation">[</span>j<span class="token punctuation">]</span> <span class="token operator">=</span> nums<span class="token punctuation">[</span>j<span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">;</span>                                                            
                nums<span class="token punctuation">[</span>j <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">]</span> <span class="token operator">=</span> temp<span class="token punctuation">;</span>                                                             
                <span class="token comment">// 只要发送过一次交换, 那就记录                                                 </span>
                flag <span class="token operator">=</span> <span class="token boolean">true</span><span class="token punctuation">;</span>                                                                    
            <span class="token punctuation">&#125;</span>                                                                                   
        <span class="token punctuation">&#125;</span>                                                                                       
        <span class="token comment">// 如果一次交换没发生, 说明后面排好序了, 直接退出                                       </span>
        <span class="token keyword">if</span><span class="token punctuation">(</span>flag<span class="token punctuation">)</span> <span class="token keyword">break</span><span class="token punctuation">;</span>                                                                         
    <span class="token punctuation">&#125;</span>                                                                                           <span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></li>
</ul>
</li>
<li><p><strong>归并排序</strong>:</p>
<ul>
<li><p><strong>其独特的适用场景: 适用于链表排序(同样适用于链表的还有直接插入排序)</strong></p>
</li>
<li><p>时间复杂度: O(nlogn)</p>
</li>
<li><p>空间复杂度是 O(logn)</p>
</li>
<li><p>思想：先把数组自顶向下不断对半拆分，就跟一颗树一样，然后自底向上两两按序合并</p>
</li>
<li><p>代码实现：</p>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">int</span><span class="token punctuation">[</span><span class="token punctuation">]</span> temp<span class="token punctuation">;</span>                                                                                
<span class="token keyword">public</span> <span class="token keyword">int</span><span class="token punctuation">[</span><span class="token punctuation">]</span> <span class="token function">sortArray</span><span class="token punctuation">(</span><span class="token keyword">int</span><span class="token punctuation">[</span><span class="token punctuation">]</span> nums<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>                                                       
    <span class="token comment">// 归并排序                                                                            </span>
    temp <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token keyword">int</span><span class="token punctuation">[</span>nums<span class="token punctuation">.</span>length<span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token comment">// 用作后续合并时暂存值                                    </span>
    <span class="token function">mergeSort</span><span class="token punctuation">(</span>nums<span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">,</span>nums<span class="token punctuation">.</span>length <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>                                                     
    <span class="token keyword">return</span> nums<span class="token punctuation">;</span>                                                                           
<span class="token punctuation">&#125;</span>                                                                                          
<span class="token keyword">private</span> <span class="token keyword">void</span> <span class="token function">mergeSort</span><span class="token punctuation">(</span><span class="token keyword">int</span><span class="token punctuation">[</span><span class="token punctuation">]</span> nums<span class="token punctuation">,</span> <span class="token keyword">int</span> left<span class="token punctuation">,</span> <span class="token keyword">int</span> right<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>                                  
    <span class="token keyword">if</span><span class="token punctuation">(</span>left <span class="token operator">>=</span> right<span class="token punctuation">)</span> <span class="token keyword">return</span><span class="token punctuation">;</span>                                                              
    <span class="token keyword">int</span> mid <span class="token operator">=</span> left <span class="token operator">+</span> <span class="token punctuation">(</span>right <span class="token operator">-</span> left<span class="token punctuation">)</span> <span class="token operator">/</span> <span class="token number">2</span><span class="token punctuation">;</span>                                                   
    <span class="token comment">// 对两边分别归并排序                                                                  </span>
    <span class="token function">mergeSort</span><span class="token punctuation">(</span>nums<span class="token punctuation">,</span>left<span class="token punctuation">,</span>mid<span class="token punctuation">)</span><span class="token punctuation">;</span>                                                              
    <span class="token function">mergeSort</span><span class="token punctuation">(</span>nums<span class="token punctuation">,</span>mid <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">,</span>right<span class="token punctuation">)</span><span class="token punctuation">;</span>                                                         
    <span class="token comment">// 对两边的归并排序结果进行合并                                                        </span>
    <span class="token keyword">int</span> i <span class="token operator">=</span> left<span class="token punctuation">,</span> j <span class="token operator">=</span> mid <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">,</span> cur <span class="token operator">=</span> left<span class="token punctuation">;</span>                                                 
    <span class="token keyword">while</span><span class="token punctuation">(</span>i <span class="token operator">&lt;=</span> mid <span class="token operator">||</span> j <span class="token operator">&lt;=</span> right<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>                                                        
        <span class="token comment">// 因为是从小到大排序, 所以每次选较小的那个放入数组中                              </span>
        <span class="token keyword">int</span> numLeft <span class="token operator">=</span> i <span class="token operator">&lt;=</span> mid <span class="token operator">?</span> nums<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">:</span> <span class="token class-name">Integer</span><span class="token punctuation">.</span>MAX_VALUE<span class="token punctuation">;</span>                              
        <span class="token keyword">int</span> numRight <span class="token operator">=</span> j <span class="token operator">&lt;=</span> right <span class="token operator">?</span> nums<span class="token punctuation">[</span>j<span class="token punctuation">]</span> <span class="token operator">:</span> <span class="token class-name">Integer</span><span class="token punctuation">.</span>MAX_VALUE<span class="token punctuation">;</span>                           
        <span class="token keyword">if</span><span class="token punctuation">(</span>numLeft <span class="token operator">&lt;=</span> numRight<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>                                                          
            temp<span class="token punctuation">[</span>cur<span class="token operator">++</span><span class="token punctuation">]</span> <span class="token operator">=</span> numLeft<span class="token punctuation">;</span>                                                         
            i<span class="token operator">++</span><span class="token punctuation">;</span>                                                                           
        <span class="token punctuation">&#125;</span> <span class="token keyword">else</span> <span class="token punctuation">&#123;</span>                                                                           
            temp<span class="token punctuation">[</span>cur<span class="token operator">++</span><span class="token punctuation">]</span> <span class="token operator">=</span> numRight<span class="token punctuation">;</span>                                                        
            j<span class="token operator">++</span><span class="token punctuation">;</span>                                                                           
        <span class="token punctuation">&#125;</span>                                                                                  
    <span class="token punctuation">&#125;</span>                                                                                      
    <span class="token comment">// 把temp[left,right]这部分的值赋值给nums                                              </span>
    <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> k <span class="token operator">=</span> left<span class="token punctuation">;</span> k <span class="token operator">&lt;=</span> right<span class="token punctuation">;</span> k<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>                                                  
        nums<span class="token punctuation">[</span>k<span class="token punctuation">]</span> <span class="token operator">=</span> temp<span class="token punctuation">[</span>k<span class="token punctuation">]</span><span class="token punctuation">;</span>                                                                 
    <span class="token punctuation">&#125;</span>                                                                                      
<span class="token punctuation">&#125;</span>                                                                                          <span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
</li>
<li><p>题型: 148,912</p>
</li>
</ul>
</li>
<li><p><strong>快速排序:</strong></p>
<ul>
<li><p>时间复杂度: O(nlogn), <strong>未随机化的话,最差O(n^2)</strong></p>
</li>
<li><p>空间复杂度: O(logn) , <strong>最差O(n)</strong></p>
</li>
<li><p>思想：每次选择一个基准数, 一般都是以left位置上的值作为基准数<br>设置左右指针, 均与基准数作对比来进行遍历, 最终得到一个位置(设为i), 而后把left与该i的值进行交换<br>从而使: <strong>i左边的所有数都小于基准值,i右边所有数都大于基准值(此时基准值就是 i位置上的数,因为交换过了)</strong><br>由i划分成两个区间[left,i-1],[i+1,right],从而进行递归,把每个区间都进行这样的排序,从而实现整体排序</p>
</li>
<li><p>代码实现：<strong>要注意i,j双指针的先后遍历顺序</strong></p>
<pre class="line-numbers language-none"><code class="language-none">private void quickSort(int[] arr, int left, int right) &#123;                               
    &#x2F;&#x2F; 子数组长度为 1 时终止递归                                                       
    if (left &gt;&#x3D; right) return;                                                         
    &#x2F;&#x2F; 寻找i位置: 以 arr[l] 作为基准数                                                 
    int i &#x3D; left, j &#x3D; right;                                                           
    while (i &lt; j) &#123;                                                                    
        &#x2F;&#x2F; 注意: 这两句while不能交换:                                                  
        &#x2F;&#x2F; 因为:                                                                       
        &#x2F;&#x2F; 假设[left,right]中, 所有元素都&gt;&#x3D;[left], 则:                                 
        &#x2F;&#x2F; 如果是先j--,后i++, 最后 j&#x3D;i&#x3D;left, 确保了i的右边都大于等于[left],i左边(虽然此时是0)都小于等于[left], 此后swap(i,left)后的两边数组再进行快排就不会出错, 因为[left]去到了正确的位置
        &#x2F;&#x2F; 如果是先i++,后j--, 由于都比left大, 那么i很可能执行完后到达left+1的位置, 而j可以在一次while中到达i, 此时循环跳出, 交换i与left的值, 但是因为[i]&gt;[left], 交换后, 就会出现i的左边的数比基准值大, 这就不符合快排的逻辑了
        &#x2F;&#x2F;      例如: 1,3,4,2 &#x3D;&gt; 当i走时, 会走到3位置, j走时也会走到3位置, 此时交换i与l
        &#x2F;&#x2F; 如果硬要交换这两句,这要把基准值从[left]换为[right]                          
                                                                                       
        &#x2F;&#x2F; 比基准值大的放在右边 (如果要实现从大到小降序排序, 则只需要把i,j的那个大小号
        while (i &lt; j &amp;&amp; arr[j] &gt;&#x3D; arr[left]) j--;                                      
        &#x2F;&#x2F; 比基准值小的放在左边                                                        
        while (i &lt; j &amp;&amp; arr[i] &lt;&#x3D; arr[left]) i++;                                      
        &#x2F;&#x2F; 找到了最终不符合条件(i左边的所有数都小于基准值,i右边所有数都大于基准值)的i与
        &#x2F;&#x2F; 则交换两者的值,使其满足条件                                                 
        swap(arr, i, j);                                                               
    &#125;                                                                                  
    &#x2F;&#x2F; 找到i位置后,交换left基准值与i位置的值,使i位置的值就是基准值                     
    swap(arr, i, left);                                                                
    &#x2F;&#x2F; 递归左右子数组执行快速排序                                                      
    quickSort(arr, left, i - 1);                                                       
    quickSort(arr, i + 1, right);                                                      
&#125;                                                                                      
private void swap(int[] arr, int i, int j) &#123;                                           
    int tmp &#x3D; arr[i];                                                                  
    arr[i] &#x3D; arr[j];                                                                   
    arr[j] &#x3D; tmp;                                                                      
&#125;                                                                                                                                                                                 <span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
</li>
<li><p>改进:</p>
<ul>
<li><p>原因及措施:<br>之前我们都是固定取left作为基准值,<br>但是如果这样的话, <strong>当出现一个顺序数组时, j–就会走完整个长度, 然后递归右边的n-1长度的数组, 以此类推, 这样时间复杂度就会退化成O(n^2)</strong><br>所以<strong>正常的快排, 最坏时间复杂度是O(n^2),</strong><br>所以对于此,我们可以引入随机数算法:<br>就是在 [0,长度-1] (长度&#x3D;right-left+1) 获得一个随机数,其实这就是偏移量,然后<strong>left+偏移量,其实就相当于从[left,right]随机取一个数作为基准值</strong><br>这样的话, <strong>是可以加速整个过程的, 通过数学分析, 引入随机数算法后的快排的最坏情况下时间复杂度只需O(n).</strong>                                      </p>
</li>
<li><p>实现: 只需要在<strong>quickSort()一开始加入以下代码</strong>:                                                                                                </p>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">int</span> temp <span class="token operator">=</span> random<span class="token punctuation">.</span><span class="token function">nextInt</span><span class="token punctuation">(</span>right <span class="token operator">-</span> left <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token operator">+</span> left<span class="token punctuation">;</span> <span class="token comment">// random是Random类的实例     </span>
<span class="token function">swap</span><span class="token punctuation">(</span>nums<span class="token punctuation">,</span>temp<span class="token punctuation">,</span>left<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 为了保持之前快排的代码逻辑, 我们事先把这个随机出来的基准值换到left位置上                             </span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre></li>
</ul>
</li>
<li><p>题型: 215(topK), 剑指offer-28,912</p>
</li>
</ul>
</li>
<li><p><strong>堆排序（会涉及手写堆与堆排）:</strong></p>
<ul>
<li><p>时间复杂度: O(nlogn)                                                                                             </p>
</li>
<li><p>空间复杂度: O(1)                                                                                                 </p>
</li>
<li><p>思想:<br>堆是一颗完全二叉树，树中每个结点的值都不小于（或不大于）其左右孩子的值。<br>如果父亲结点是大于等于左右孩子就是大顶堆，小于等于左右孩子就是小顶堆。                                       </p>
</li>
<li><p>借助 <strong>优先队列PriorityQueue</strong> 来实现:<br>可以考虑使用 优先队列PriorityQueue 来作为堆结构的实现<br>当我们传递的new Comparator是:<br>​         前-后时,意味着从小到大排序-&gt;小顶堆<br>​         后-前时,意味着从大到小排序-&gt;大顶堆<br>​    我们只需把要排序的元素, 逐一存入 优先队列中, 就相当于自动完成了堆结构<br>​    当我们每次从优先队列poll出来的元素时, 其实就是当前的最大值或最小值, 然后优先队列会自动又调整堆,使其恢复堆结构<br>​    然后我们依次从中取出元素, 每次取出都是最大值&#x2F;最小值, 存起来, 即可完成堆排序<br>​    (注意: <strong>优先队列仅仅只是堆的自动构建,并不等于堆排序</strong>)                                                      </p>
<p>借助PriorityQueue进行排序的 <strong>应用场景</strong>:  要进行排序,但是**还要动态地控制参与排序的元素 **<br>(以下回答,摘自 栈与队列专题训练-07-前k个高频元素 的题解: )<br>为什么采用优先队列进行排序, 而不是用快速排序,归并排序等这些时间复杂度差不多的?<br>    其实本题也可以使用, 只需要把Map转化为数组, 然后使用即可<br>    但是, 因为本题结果只需要k个元素, 而不是全部元素(设为n),<br>    如果使用快排等排序, 那就只能全部进行排序,然后事后进行取舍出k个元素,也就是说排序过程中始终都在维护n个元素<br>    但是, 如果使用优先队列的话, 通过队列的特性, 我们可以通过offer,poll等来控制队列内参与排序的元素<br>    <strong>从而就能控制排序过程中始终只有k个元素在进行,而不一定要是n个</strong>        </p>
</li>
<li><p><strong>手写堆 与 手写堆排序: 重点 (注意: 手写堆并不代表堆排序，堆排序是借助堆的性质进行排序，手写堆是手写一个堆数据结构，堆内部不一定是排好序的)</strong>                                                                          </p>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Solution</span> <span class="token punctuation">&#123;</span>                                                                               
    <span class="token comment">/*                                                                                                
        堆结构:                                                                                       
            堆其实是一个特殊的完全二叉树, 对于完全二叉树而言, 其实一个连续的数组, 可以等同于完全二叉树
            因为连续数组对应的二叉树, 必然是只会在右边缺叶子节点, 这符合完全二叉树的定义              
            从而,对于数组中的i号元素而言:                                                             
            左孩子: 2*i + 1                                                                           
            右孩子: 2*i + 2                                                                           
            父节点: (i - 1) / 2                                                                       
            所以, 我们完全可以通过数组来作为堆的存储结构, 而通过下标实现父子关系并通过一些处理来形成逻
            而heapSize其实就是当前堆的实际长度, 也是下一个元素要插入的位置                            
            堆的特殊所在:                                                                             
                如果是大根堆, 则任何一个子树, 其根节点都是该子树的最大值                              
                如果是小跟堆, 则任何一个子树, 其根节点都是该子树的最小值                              
        **堆的两个基本操作**: 几乎所有的堆操作都是基于这两个中的一个来实现的                          
        (下面以大根堆来作为说明)                                                                      
            1. 堆插入heapInsert: 对于当前现存的堆(其实是数组), 插入一个元素, 如何调整为堆             
                      思路: 因为堆的含义是: 任何一个子树, 其根节点都是其下面子树的最大值              
                           换个角度, 则对于任何一个节点, 其都应该是下面子树的最大值                   
                           所以, 当我们插入一个节点时, 应该要与其父节点进行对比, 如果大于父节点, 要进
                      实现: 根据之前说的下标关系, 我们可以通过 (index-1)/2来找到父节点, 然后while循环
            2. 堆化heapify: 对于当前给定的根节点(index), 使其下面的子树堆化                           
                      思路: 和上面类似, 只不过上面的是插入到末尾然后要向上确定该节点应该所处的根节点位
                           而此时是把下面的子树堆化, 也就是要一步步选举下面的子树的根节点, 所以要向下
                           所以, 当我们是要对 index 与 其左右子节点, 选举一个最大值, 然后交换最大值作
                      实现: 根据之前说的下标关系, 我们可以通过 index*2+1 与 index*2+2得到左右子节点, w
        (小根堆就是把上面所说的一些大于 改成 小于,求最大值改成求最小值 即可)                          
        **堆的对外发布的主要操作: 可参考PriorityQueue优先队列**                                       
            1. offer: 入队 => 新增一个元素                                                            
                实现: heapSize指向的就是应该插入的位置, 所以插入后, heapSize++, 然后调用**堆插入heapIn
            2. poll: 出队: 取出最大值 => 删除根节点                                                   
                实现: 当前0号元素就是最大值, 将当前0号元素出队, 且交换0号元素与最后元素,**heapSize-1使
                      因为此时最后一个元素换到了根节点, 所以要进行**堆化heapify**                     
                                                                                                      
        以上是**手写堆结构的全部内容**, 我们可以通过手写堆排序来实现像topK这种题目                    
        但是注意: **手写堆 并不等于 堆排序, 堆的结构并不代表着排序后的结果**                          
        例如: 8 4 1 2 是一个堆, 但是很明显, 他并不排好序啊                                            
        所以说, **我们下面要单独介绍一下堆排序, 其实堆排序和其他堆操作一样, 只需要基于堆插入,堆化两个
                                                                                                      
        堆排序: 借助逻辑上的堆结构, 实现一个排序                                                      
            1. 整体思想:                                                                              
                    仔细想想堆的定义: 当前根节点是整个子树的最大值/最小值                             
                    也就是说, **我们每一次都可以从堆的0号元素拿到最大值/最小值**,                     
                    那么, **如果我们每次取其0号元素,放到当前末尾, 然后对[0,当前末尾-1]再次堆化,以此类
                    **是不是就可以每次都取到最大值/最小值, 放到最后, 这样整个遍历完, 是不是这就是从小
            2. 实现:                                                                                  
                    0) 根据上面的思想, 我们可以知道: 从小到大排序=>大根堆, 从大到小排序=> 小根堆      
                       所以我们先选择大根堆/小根堆, 然后实现其对应的 堆插入 与 堆化 方法              
                    1) 我们先把整个数组转化为堆, 有两个方法:                                          
                        第一种: 我们可以认为当前数组是一个个插入堆中的, 所以就可以顺序遍历, 然后逐个调
                        第二种: 我们可以认为当前数组他就是一个完全二叉树,只不过没堆化罢了, 所以我们从
                        (推荐使用第二种, 因为可以把这一步的时间复杂度缩小到O(n))                      
                    2) 取当前堆的根节点, 即:0号元素, 换到当前最后位置(--heapSize),                    
                       对剩余的[0,heapSize)进行堆化 => 其实就是对0号元素(根节点)以下的整个树进行堆化(
                       以此类推,直到heapSize==0, 此时整个堆都已经遍历结束, 所以排序也就排好了         
               (**你会发现, 其实2)只用到了heapify一个方法, 如果我们1)采用的是第二个方法, 则整个堆排只
     */</span>                                                                                               
    <span class="token comment">// 手写堆结构                                                                                     </span>
    <span class="token keyword">class</span> <span class="token class-name">MyMaxHeap</span> <span class="token punctuation">&#123;</span>                                                                                 
        <span class="token keyword">private</span> <span class="token class-name">List</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Integer</span><span class="token punctuation">></span></span> list<span class="token punctuation">;</span>                                                                   
        <span class="token keyword">private</span> <span class="token keyword">int</span> heapSize <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>                                                                     
                                                                                                      
        <span class="token keyword">public</span> <span class="token class-name">MyMaxHeap</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>                                                                          
            list <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ArrayList</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token punctuation">></span></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>                                                                 
        <span class="token punctuation">&#125;</span>                                                                                             
        <span class="token keyword">public</span> <span class="token class-name">MyMaxHeap</span><span class="token punctuation">(</span><span class="token keyword">int</span><span class="token punctuation">[</span><span class="token punctuation">]</span> arr<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>                                                                 
            <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> arr<span class="token punctuation">.</span>length<span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>                                                    
                list<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span>arr<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>                                                                     
            <span class="token punctuation">&#125;</span>                                                                                         
            <span class="token function">initHeap</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>                                                                               
        <span class="token punctuation">&#125;</span>                                                                                             
        <span class="token comment">// 堆初始化: 把数组先整体变为堆                                                               </span>
        <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">initHeap</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>                                                                      
            heapSize <span class="token operator">=</span> list<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>                                                                   
            <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> list<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">;</span> i <span class="token operator">>=</span> <span class="token number">0</span><span class="token punctuation">;</span> i<span class="token operator">--</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>                                              
                <span class="token function">heapify</span><span class="token punctuation">(</span>i<span class="token punctuation">)</span><span class="token punctuation">;</span>                                                                           
            <span class="token punctuation">&#125;</span>                                                                                         
        <span class="token punctuation">&#125;</span>                                                                                             
        <span class="token comment">// 堆判空                                                                                     </span>
        <span class="token keyword">public</span> <span class="token keyword">boolean</span> <span class="token function">isEmpty</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>                                                                    
            <span class="token keyword">return</span> heapSize <span class="token operator">&lt;=</span> <span class="token number">0</span><span class="token punctuation">;</span>                                                                     
        <span class="token punctuation">&#125;</span>                                                                                             
        <span class="token comment">// 往堆加入新元素                                                                             </span>
        <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">offer</span><span class="token punctuation">(</span><span class="token keyword">int</span> value<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>                                                                
            <span class="token comment">// 因为当我们poll时,采取的策略是: 让heapSize--从而使得交换到最后的元素无效化,这样一来list</span>
            <span class="token comment">// 所以此时应该是set,把heapSize的位置修改, 而不应该是add,add的话会导致无效化 无效         </span>
            <span class="token keyword">if</span><span class="token punctuation">(</span>list<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">></span> heapSize<span class="token punctuation">)</span> list<span class="token punctuation">.</span><span class="token function">set</span><span class="token punctuation">(</span>heapSize<span class="token punctuation">,</span>value<span class="token punctuation">)</span><span class="token punctuation">;</span>                                      
            <span class="token keyword">else</span> list<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span>value<span class="token punctuation">)</span><span class="token punctuation">;</span>                                                                     
            <span class="token function">heapInsert</span><span class="token punctuation">(</span>heapSize<span class="token operator">++</span><span class="token punctuation">)</span><span class="token punctuation">;</span>                                                                   
        <span class="token punctuation">&#125;</span>                                                                                             
        <span class="token comment">// 把堆的头元素出队, 即:取出最大值                                                            </span>
        <span class="token keyword">public</span> <span class="token keyword">int</span> <span class="token function">poll</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>                                                                           
            <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token function">isEmpty</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token keyword">throw</span> <span class="token keyword">new</span> <span class="token class-name">ArrayIndexOutOfBoundsException</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>                                 
            <span class="token comment">// 当前0号元素就是最大值, 将当前0号元素出队, 且交换0号元素与最后元素, heapSize-1使其无效化</span>
            <span class="token comment">// 因为此时最后一个元素换到了根节点, 所以要进行堆化                                       </span>
            <span class="token keyword">int</span> root <span class="token operator">=</span> list<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>                                                                   
            <span class="token function">swap</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token operator">--</span>heapSize<span class="token punctuation">)</span><span class="token punctuation">;</span>                                                                       
            <span class="token comment">// 对0号元素以下的子树进行堆化, 就相当于对整个树进行堆化                                  </span>
            <span class="token function">heapify</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>                                                                               
            <span class="token keyword">return</span> root<span class="token punctuation">;</span>                                                                              
        <span class="token punctuation">&#125;</span>                                                                                             
        <span class="token comment">// 获取最大值                                                                                 </span>
        <span class="token keyword">public</span> <span class="token keyword">int</span> <span class="token function">peek</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>                                                                           
            <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token function">isEmpty</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token keyword">throw</span> <span class="token keyword">new</span> <span class="token class-name">ArrayIndexOutOfBoundsException</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>                                 
            <span class="token keyword">return</span> list<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>                                                                       
        <span class="token punctuation">&#125;</span>                                                                                             
        <span class="token comment">// 获取长度                                                                                   </span>
        <span class="token keyword">public</span> <span class="token keyword">int</span> <span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>                                                                           
            <span class="token keyword">return</span> heapSize<span class="token punctuation">;</span>                                                                          
        <span class="token punctuation">&#125;</span>                                                                                             
        <span class="token comment">// 堆插入                                                                                     </span>
        <span class="token keyword">private</span> <span class="token keyword">void</span> <span class="token function">heapInsert</span><span class="token punctuation">(</span><span class="token keyword">int</span> index<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>                                                          
            <span class="token comment">// 不断与父节点进行对比,并交换                                                            </span>
            <span class="token keyword">while</span><span class="token punctuation">(</span>list<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span>index<span class="token punctuation">)</span> <span class="token operator">></span> list<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token punctuation">(</span>index <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token operator">/</span> <span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>                                      
                <span class="token function">swap</span><span class="token punctuation">(</span>index<span class="token punctuation">,</span><span class="token punctuation">(</span>index <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token operator">/</span> <span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span>                                                          
                index <span class="token operator">=</span> <span class="token punctuation">(</span>index <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token operator">/</span> <span class="token number">2</span><span class="token punctuation">;</span>                                                              
            <span class="token punctuation">&#125;</span>                                                                                         
        <span class="token punctuation">&#125;</span>                                                                                             
        <span class="token comment">// 堆化                                                                                       </span>
        <span class="token keyword">private</span> <span class="token keyword">void</span> <span class="token function">heapify</span><span class="token punctuation">(</span><span class="token keyword">int</span> index<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>                                                             
            <span class="token comment">// 获取左节点                                                                             </span>
            <span class="token keyword">int</span> left <span class="token operator">=</span> index <span class="token operator">*</span> <span class="token number">2</span> <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">;</span>                                                                 
            <span class="token keyword">while</span><span class="token punctuation">(</span>left <span class="token operator">&lt;</span> heapSize<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span> <span class="token comment">// 因为右节点会比左节点后, 所以判断左节点即可确定index下面有没有</span>
                <span class="token comment">// 选举 左,右,根 中最大的那一个                                                       </span>
                <span class="token keyword">int</span> largest <span class="token operator">=</span> left <span class="token operator">+</span> <span class="token number">1</span> <span class="token operator">&lt;</span> heapSize <span class="token operator">&amp;&amp;</span> list<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span>left <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token operator">></span> list<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span>left<span class="token punctuation">)</span> <span class="token operator">?</span> left <span class="token operator">+</span> <span class="token number">1</span> <span class="token operator">:</span> 
                largest <span class="token operator">=</span> list<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span>largest<span class="token punctuation">)</span> <span class="token operator">></span> list<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span>index<span class="token punctuation">)</span> <span class="token operator">?</span> largest <span class="token operator">:</span> index<span class="token punctuation">;</span>                      
                <span class="token comment">// 如果当前根节点就是下面子树最大值, 则无需再往下了, 跳出循环                         </span>
                <span class="token keyword">if</span><span class="token punctuation">(</span>largest <span class="token operator">==</span> index<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>                                                                
                    <span class="token keyword">break</span><span class="token punctuation">;</span>                                                                            
                <span class="token punctuation">&#125;</span>                                                                                     
                <span class="token comment">// 否则, 迭代                                                                         </span>
                <span class="token function">swap</span><span class="token punctuation">(</span>largest<span class="token punctuation">,</span>index<span class="token punctuation">)</span><span class="token punctuation">;</span>                                                                  
                index <span class="token operator">=</span> largest<span class="token punctuation">;</span>                                                                      
                left <span class="token operator">=</span> index <span class="token operator">*</span> <span class="token number">2</span> <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">;</span>                                                                 
            <span class="token punctuation">&#125;</span>                                                                                         
        <span class="token punctuation">&#125;</span>                                                                                             
        <span class="token comment">// 交换函数                                                                                   </span>
        <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">swap</span><span class="token punctuation">(</span><span class="token keyword">int</span> i<span class="token punctuation">,</span><span class="token keyword">int</span> j<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>                                                               
            <span class="token class-name">Collections</span><span class="token punctuation">.</span><span class="token function">swap</span><span class="token punctuation">(</span>list<span class="token punctuation">,</span>i<span class="token punctuation">,</span>j<span class="token punctuation">)</span><span class="token punctuation">;</span>                                                               
        <span class="token punctuation">&#125;</span>                                                                                             
    <span class="token punctuation">&#125;</span>                                                                                                 
                                                                                                      
    <span class="token keyword">class</span> <span class="token class-name">MyMinHeap</span> <span class="token punctuation">&#123;</span>                                                                                 
        <span class="token keyword">private</span> <span class="token class-name">List</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Integer</span><span class="token punctuation">></span></span> list<span class="token punctuation">;</span>                                                                   
        <span class="token keyword">private</span> <span class="token keyword">int</span> heapSize <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>                                                                     
                                                                                                      
        <span class="token keyword">public</span> <span class="token class-name">MyMinHeap</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>                                                                          
            list <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ArrayList</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token punctuation">></span></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>                                                                 
        <span class="token punctuation">&#125;</span>                                                                                             
        <span class="token keyword">public</span> <span class="token class-name">MyMinHeap</span><span class="token punctuation">(</span><span class="token keyword">int</span><span class="token punctuation">[</span><span class="token punctuation">]</span> arr<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>                                                                 
            <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> arr<span class="token punctuation">.</span>length<span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>                                                    
                list<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span>arr<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>                                                                     
            <span class="token punctuation">&#125;</span>                                                                                         
            <span class="token function">initHeap</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>                                                                               
        <span class="token punctuation">&#125;</span>                                                                                             
        <span class="token comment">// 堆初始化: 把数组先整体变为堆                                                               </span>
        <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">initHeap</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>                                                                      
            heapSize <span class="token operator">=</span> list<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>                                                                   
            <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> list<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">;</span> i <span class="token operator">>=</span> <span class="token number">0</span><span class="token punctuation">;</span> i<span class="token operator">--</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>                                              
                <span class="token function">heapify</span><span class="token punctuation">(</span>i<span class="token punctuation">)</span><span class="token punctuation">;</span>                                                                           
            <span class="token punctuation">&#125;</span>                                                                                         
        <span class="token punctuation">&#125;</span>                                                                                             
        <span class="token comment">// 堆判空                                                                                     </span>
        <span class="token keyword">public</span> <span class="token keyword">boolean</span> <span class="token function">isEmpty</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>                                                                    
            <span class="token keyword">return</span> heapSize <span class="token operator">&lt;=</span> <span class="token number">0</span><span class="token punctuation">;</span>                                                                     
        <span class="token punctuation">&#125;</span>                                                                                             
        <span class="token comment">// 往堆加入新元素                                                                             </span>
        <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">offer</span><span class="token punctuation">(</span><span class="token keyword">int</span> value<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>                                                                
            <span class="token comment">// 因为当我们poll时,采取的策略是: 让heapSize--从而使得交换到最后的元素无效化,这样一来list</span>
            <span class="token comment">// 所以此时应该是set,把heapSize的位置修改, 而不应该是add,add的话会导致无效化 无效         </span>
            <span class="token keyword">if</span><span class="token punctuation">(</span>list<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">></span> heapSize<span class="token punctuation">)</span> list<span class="token punctuation">.</span><span class="token function">set</span><span class="token punctuation">(</span>heapSize<span class="token punctuation">,</span>value<span class="token punctuation">)</span><span class="token punctuation">;</span>                                      
            <span class="token keyword">else</span> list<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span>value<span class="token punctuation">)</span><span class="token punctuation">;</span>                                                                     
            <span class="token function">heapInsert</span><span class="token punctuation">(</span>heapSize<span class="token operator">++</span><span class="token punctuation">)</span><span class="token punctuation">;</span>                                                                   
        <span class="token punctuation">&#125;</span>                                                                                             
        <span class="token comment">// 把堆的头元素出队, 即:取出最大值                                                            </span>
        <span class="token keyword">public</span> <span class="token keyword">int</span> <span class="token function">poll</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>                                                                           
            <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token function">isEmpty</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token keyword">throw</span> <span class="token keyword">new</span> <span class="token class-name">ArrayIndexOutOfBoundsException</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>                                 
            <span class="token comment">// 当前0号元素就是最大值, 将当前0号元素出队, 且交换0号元素与最后元素, heapSize-1使其无效化</span>
            <span class="token comment">// 因为此时最后一个元素换到了根节点, 所以要进行堆化                                       </span>
            <span class="token keyword">int</span> root <span class="token operator">=</span> list<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>                                                                   
            <span class="token function">swap</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token operator">--</span>heapSize<span class="token punctuation">)</span><span class="token punctuation">;</span>                                                                       
            <span class="token function">heapify</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>                                                                               
            <span class="token keyword">return</span> root<span class="token punctuation">;</span>                                                                              
        <span class="token punctuation">&#125;</span>                                                                                             
        <span class="token comment">// 获取最大值                                                                                 </span>
        <span class="token keyword">public</span> <span class="token keyword">int</span> <span class="token function">peek</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>                                                                           
            <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token function">isEmpty</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token keyword">throw</span> <span class="token keyword">new</span> <span class="token class-name">ArrayIndexOutOfBoundsException</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>                                 
            <span class="token keyword">return</span> list<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>                                                                       
        <span class="token punctuation">&#125;</span>                                                                                             
        <span class="token comment">// 获取长度                                                                                   </span>
        <span class="token keyword">public</span> <span class="token keyword">int</span> <span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>                                                                           
            <span class="token keyword">return</span> heapSize<span class="token punctuation">;</span>                                                                          
        <span class="token punctuation">&#125;</span>                                                                                             
        <span class="token comment">// 堆插入                                                                                     </span>
        <span class="token keyword">private</span> <span class="token keyword">void</span> <span class="token function">heapInsert</span><span class="token punctuation">(</span><span class="token keyword">int</span> index<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>                                                          
            <span class="token comment">// 不断与父节点进行对比,并交换                                                            </span>
            <span class="token keyword">while</span><span class="token punctuation">(</span>list<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span>index<span class="token punctuation">)</span> <span class="token operator">&lt;</span> list<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token punctuation">(</span>index <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token operator">/</span> <span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>                                      
                <span class="token function">swap</span><span class="token punctuation">(</span>index<span class="token punctuation">,</span><span class="token punctuation">(</span>index <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token operator">/</span> <span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span>                                                          
                index <span class="token operator">=</span> <span class="token punctuation">(</span>index <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token operator">/</span> <span class="token number">2</span><span class="token punctuation">;</span>                                                              
            <span class="token punctuation">&#125;</span>                                                                                         
        <span class="token punctuation">&#125;</span>                                                                                             
        <span class="token comment">// 堆化                                                                                       </span>
        <span class="token keyword">private</span> <span class="token keyword">void</span> <span class="token function">heapify</span><span class="token punctuation">(</span><span class="token keyword">int</span> index<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>                                                             
            <span class="token comment">// 获取左节点                                                                             </span>
            <span class="token keyword">int</span> left <span class="token operator">=</span> index <span class="token operator">*</span> <span class="token number">2</span> <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">;</span>                                                                 
            <span class="token keyword">while</span><span class="token punctuation">(</span>left <span class="token operator">&lt;</span> heapSize<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span> <span class="token comment">// 因为右节点会比左节点后, 所以判断左节点即可确定index下面有没有</span>
                <span class="token comment">// 选举 左,右,根 中最大的那一个                                                       </span>
                <span class="token keyword">int</span> min <span class="token operator">=</span> left <span class="token operator">+</span> <span class="token number">1</span> <span class="token operator">&lt;</span> heapSize <span class="token operator">&amp;&amp;</span> list<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span>left <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token operator">&lt;</span> list<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span>left<span class="token punctuation">)</span> <span class="token operator">?</span> left <span class="token operator">+</span> <span class="token number">1</span> <span class="token operator">:</span> left
                min <span class="token operator">=</span> list<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span>min<span class="token punctuation">)</span> <span class="token operator">&lt;</span> list<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span>index<span class="token punctuation">)</span> <span class="token operator">?</span> min <span class="token operator">:</span> index<span class="token punctuation">;</span>                                  
                <span class="token comment">// 如果当前根节点就是下面子树最大值, 则无需再往下了, 跳出循环                         </span>
                <span class="token keyword">if</span><span class="token punctuation">(</span>min <span class="token operator">==</span> index<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>                                                                    
                    <span class="token keyword">break</span><span class="token punctuation">;</span>                                                                            
                <span class="token punctuation">&#125;</span>                                                                                     
                <span class="token comment">// 否则, 迭代                                                                         </span>
                <span class="token function">swap</span><span class="token punctuation">(</span>min<span class="token punctuation">,</span>index<span class="token punctuation">)</span><span class="token punctuation">;</span>                                                                      
                index <span class="token operator">=</span> min<span class="token punctuation">;</span>                                                                          
                left <span class="token operator">=</span> index <span class="token operator">*</span> <span class="token number">2</span> <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">;</span>                                                                 
            <span class="token punctuation">&#125;</span>                                                                                         
        <span class="token punctuation">&#125;</span>                                                                                             
        <span class="token comment">// 交换函数                                                                                   </span>
        <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">swap</span><span class="token punctuation">(</span><span class="token keyword">int</span> i<span class="token punctuation">,</span><span class="token keyword">int</span> j<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>                                                               
            <span class="token class-name">Collections</span><span class="token punctuation">.</span><span class="token function">swap</span><span class="token punctuation">(</span>list<span class="token punctuation">,</span>i<span class="token punctuation">,</span>j<span class="token punctuation">)</span><span class="token punctuation">;</span>                                                               
        <span class="token punctuation">&#125;</span>                                                                                             
    <span class="token punctuation">&#125;</span>                                                                                                 
                                                                                                      
    <span class="token comment">// 堆排序:                                                                                        </span>
    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">heapSort</span><span class="token punctuation">(</span><span class="token keyword">int</span><span class="token punctuation">[</span><span class="token punctuation">]</span> arr<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>                                                                 
        <span class="token keyword">if</span><span class="token punctuation">(</span>arr <span class="token operator">==</span> <span class="token keyword">null</span> <span class="token operator">||</span> arr<span class="token punctuation">.</span>length <span class="token operator">&lt;</span> <span class="token number">2</span><span class="token punctuation">)</span> <span class="token keyword">return</span><span class="token punctuation">;</span>                                                     
        <span class="token comment">// 1) 把整个数组转化为堆                                                                      </span>
        <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> arr<span class="token punctuation">.</span>length <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">;</span> i <span class="token operator">>=</span> <span class="token number">0</span><span class="token punctuation">;</span> i<span class="token operator">--</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>                                                   
            <span class="token function">heapify</span><span class="token punctuation">(</span>arr<span class="token punctuation">,</span>i<span class="token punctuation">,</span>arr<span class="token punctuation">.</span>length<span class="token punctuation">)</span><span class="token punctuation">;</span>                                                                
        <span class="token punctuation">&#125;</span>                                                                                             
        <span class="token comment">// 2) 依次交换并堆化                                                                          </span>
        <span class="token keyword">int</span> heapSize <span class="token operator">=</span> arr<span class="token punctuation">.</span>length<span class="token punctuation">;</span>                                                                    
        <span class="token function">swap</span><span class="token punctuation">(</span>arr<span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token operator">--</span>heapSize<span class="token punctuation">)</span><span class="token punctuation">;</span>                                                                       
        <span class="token keyword">while</span> <span class="token punctuation">(</span>heapSize <span class="token operator">></span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>                                                                        
            <span class="token function">heapify</span><span class="token punctuation">(</span>arr<span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">,</span>heapSize<span class="token punctuation">)</span><span class="token punctuation">;</span>                                                                  
            <span class="token function">swap</span><span class="token punctuation">(</span>arr<span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token operator">--</span>heapSize<span class="token punctuation">)</span><span class="token punctuation">;</span>                                                                   
        <span class="token punctuation">&#125;</span>                                                                                             
    <span class="token punctuation">&#125;</span>                                                                                                 
    <span class="token comment">// 0) 先写选定的大根堆/小根堆 对应的heapify方法                                                   </span>
    <span class="token comment">// (注意: 此处你会发现和之前手写堆结构不一样, 那是因为手写堆结构, 这个方法的参数都在对象里,无需传</span>
    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">heapify</span><span class="token punctuation">(</span><span class="token keyword">int</span><span class="token punctuation">[</span><span class="token punctuation">]</span> arr<span class="token punctuation">,</span><span class="token keyword">int</span> index<span class="token punctuation">,</span><span class="token keyword">int</span> heapSize<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>                                           
        <span class="token keyword">int</span> left <span class="token operator">=</span> index <span class="token operator">*</span> <span class="token number">2</span> <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">;</span>                                                                     
        <span class="token keyword">while</span><span class="token punctuation">(</span>left <span class="token operator">&lt;</span> heapSize<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>                                                                      
            <span class="token keyword">int</span> largest <span class="token operator">=</span> left <span class="token operator">+</span> <span class="token number">1</span> <span class="token operator">&lt;</span> heapSize <span class="token operator">&amp;&amp;</span> arr<span class="token punctuation">[</span>left <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">]</span> <span class="token operator">></span> arr<span class="token punctuation">[</span>left<span class="token punctuation">]</span> <span class="token operator">?</span> left <span class="token operator">+</span> <span class="token number">1</span> <span class="token operator">:</span> left<span class="token punctuation">;</span>         
            largest <span class="token operator">=</span> arr<span class="token punctuation">[</span>largest<span class="token punctuation">]</span> <span class="token operator">></span> arr<span class="token punctuation">[</span>index<span class="token punctuation">]</span> <span class="token operator">?</span> largest <span class="token operator">:</span> index<span class="token punctuation">;</span>                                    
            <span class="token keyword">if</span><span class="token punctuation">(</span>largest <span class="token operator">==</span> index<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>                                                                    
                <span class="token keyword">break</span><span class="token punctuation">;</span>                                                                                
            <span class="token punctuation">&#125;</span>                                                                                         
            <span class="token function">swap</span><span class="token punctuation">(</span>arr<span class="token punctuation">,</span>largest<span class="token punctuation">,</span>index<span class="token punctuation">)</span><span class="token punctuation">;</span>                                                                  
            index <span class="token operator">=</span> largest<span class="token punctuation">;</span>                                                                          
            left <span class="token operator">=</span> index <span class="token operator">*</span> <span class="token number">2</span> <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">;</span>                                                                     
        <span class="token punctuation">&#125;</span>                                                                                             
    <span class="token punctuation">&#125;</span>                                                                                                 
    <span class="token keyword">private</span> <span class="token keyword">void</span> <span class="token function">swap</span><span class="token punctuation">(</span><span class="token keyword">int</span><span class="token punctuation">[</span><span class="token punctuation">]</span> arr<span class="token punctuation">,</span> <span class="token keyword">int</span> i<span class="token punctuation">,</span> <span class="token keyword">int</span> j<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>                                                      
        <span class="token keyword">int</span> temp <span class="token operator">=</span> arr<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">;</span>                                                                            
        arr<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">=</span> arr<span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">;</span>                                                                              
        arr<span class="token punctuation">[</span>j<span class="token punctuation">]</span> <span class="token operator">=</span> temp<span class="token punctuation">;</span>                                                                                
    <span class="token punctuation">&#125;</span>                                                                                                 
                                                                                                      
    <span class="token comment">// 测试手写的大根堆结构                                                                           </span>
    <span class="token keyword">public</span> <span class="token keyword">int</span><span class="token punctuation">[</span><span class="token punctuation">]</span> <span class="token function">minK</span><span class="token punctuation">(</span><span class="token keyword">int</span><span class="token punctuation">[</span><span class="token punctuation">]</span> arr<span class="token punctuation">,</span><span class="token keyword">int</span> k<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>                                                              
        <span class="token class-name">MyMaxHeap</span> heap <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">MyMaxHeap</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>                                                             
        <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> arr<span class="token punctuation">.</span>length<span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>                                                        
            <span class="token keyword">if</span><span class="token punctuation">(</span>heap<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">&lt;</span> k<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>                                                                     
                heap<span class="token punctuation">.</span><span class="token function">offer</span><span class="token punctuation">(</span>arr<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>                                                                   
            <span class="token punctuation">&#125;</span> <span class="token keyword">else</span> <span class="token punctuation">&#123;</span>                                                                                  
                <span class="token keyword">int</span> peek <span class="token operator">=</span> heap<span class="token punctuation">.</span><span class="token function">peek</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>                                                               
                <span class="token keyword">if</span><span class="token punctuation">(</span>arr<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">&lt;=</span> peek<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>                                                                  
                    heap<span class="token punctuation">.</span><span class="token function">poll</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>                                                                      
                    heap<span class="token punctuation">.</span><span class="token function">offer</span><span class="token punctuation">(</span>arr<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>                                                               
                <span class="token punctuation">&#125;</span>                                                                                     
            <span class="token punctuation">&#125;</span>                                                                                         
        <span class="token punctuation">&#125;</span>                                                                                             
        <span class="token keyword">int</span><span class="token punctuation">[</span><span class="token punctuation">]</span> res <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token keyword">int</span><span class="token punctuation">[</span>k<span class="token punctuation">]</span><span class="token punctuation">;</span>                                                                       
        <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> k<span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>                                                                 
            res<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">=</span> heap<span class="token punctuation">.</span><span class="token function">poll</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>                                                                     
        <span class="token punctuation">&#125;</span>                                                                                             
        <span class="token keyword">return</span> res<span class="token punctuation">;</span>                                                                                   
    <span class="token punctuation">&#125;</span>                                                                                                 
    <span class="token comment">// 测试手写的小根堆结构                                                                           </span>
    <span class="token keyword">public</span> <span class="token keyword">int</span><span class="token punctuation">[</span><span class="token punctuation">]</span> <span class="token function">topK</span><span class="token punctuation">(</span><span class="token keyword">int</span><span class="token punctuation">[</span><span class="token punctuation">]</span> arr<span class="token punctuation">,</span><span class="token keyword">int</span> k<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>                                                              
        <span class="token class-name">MyMinHeap</span> heap <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">MyMinHeap</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>                                                             
        <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> arr<span class="token punctuation">.</span>length<span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>                                                        
            <span class="token keyword">if</span><span class="token punctuation">(</span>heap<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">&lt;</span> k<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>                                                                     
                heap<span class="token punctuation">.</span><span class="token function">offer</span><span class="token punctuation">(</span>arr<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>                                                                   
            <span class="token punctuation">&#125;</span> <span class="token keyword">else</span> <span class="token punctuation">&#123;</span>                                                                                  
                <span class="token keyword">int</span> peek <span class="token operator">=</span> heap<span class="token punctuation">.</span><span class="token function">peek</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>                                                               
                <span class="token keyword">if</span><span class="token punctuation">(</span>arr<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">>=</span> peek<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>                                                                  
                    heap<span class="token punctuation">.</span><span class="token function">poll</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>                                                                      
                    heap<span class="token punctuation">.</span><span class="token function">offer</span><span class="token punctuation">(</span>arr<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>                                                               
                <span class="token punctuation">&#125;</span>                                                                                     
            <span class="token punctuation">&#125;</span>                                                                                         
        <span class="token punctuation">&#125;</span>                                                                                             
        <span class="token keyword">int</span><span class="token punctuation">[</span><span class="token punctuation">]</span> res <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token keyword">int</span><span class="token punctuation">[</span>k<span class="token punctuation">]</span><span class="token punctuation">;</span>                                                                       
        <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> k<span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>                                                                 
            res<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">=</span> heap<span class="token punctuation">.</span><span class="token function">poll</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>                                                                     
        <span class="token punctuation">&#125;</span>                                                                                             
        <span class="token keyword">return</span> res<span class="token punctuation">;</span>                                                                                   
    <span class="token punctuation">&#125;</span>                                                                                                 
                                                                                                      
    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>                                                          
        <span class="token class-name">Solution</span> s <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Solution</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>                                                                  
        <span class="token comment">// 测试大根堆                                                                                 </span>
        <span class="token keyword">int</span><span class="token punctuation">[</span><span class="token punctuation">]</span> res <span class="token operator">=</span> s<span class="token punctuation">.</span><span class="token function">minK</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token keyword">int</span><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">&#123;</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">4</span><span class="token punctuation">,</span><span class="token number">8</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">6</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">&#125;</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">;</span>                                                 
        <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> res<span class="token punctuation">.</span>length<span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span> <span class="token comment">// 3 2 1                                               </span>
            <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">print</span><span class="token punctuation">(</span>res<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">+</span> <span class="token string">" "</span><span class="token punctuation">)</span><span class="token punctuation">;</span>                                                           
        <span class="token punctuation">&#125;</span>                                                                                             
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>                                                                         
        <span class="token comment">// 测试小根堆                                                                                 </span>
        res <span class="token operator">=</span> s<span class="token punctuation">.</span><span class="token function">topK</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token keyword">int</span><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">&#123;</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">4</span><span class="token punctuation">,</span><span class="token number">8</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">6</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">&#125;</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">;</span>                                                       
        <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> res<span class="token punctuation">.</span>length<span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span> <span class="token comment">// 4 6 8                                               </span>
            <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">print</span><span class="token punctuation">(</span>res<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">+</span> <span class="token string">" "</span><span class="token punctuation">)</span><span class="token punctuation">;</span>                                                           
        <span class="token punctuation">&#125;</span>                                                                                             
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>                                                                         
        <span class="token comment">// 测试堆排序                                                                                 </span>
        <span class="token keyword">int</span><span class="token punctuation">[</span><span class="token punctuation">]</span> arr <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token keyword">int</span><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">&#123;</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">4</span><span class="token punctuation">,</span><span class="token number">8</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">6</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">&#125;</span><span class="token punctuation">;</span>                                                           
        s<span class="token punctuation">.</span><span class="token function">heapSort</span><span class="token punctuation">(</span>arr<span class="token punctuation">)</span><span class="token punctuation">;</span>                                                                              
        <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> arr<span class="token punctuation">.</span>length<span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span> <span class="token comment">// 1 2 3 4 6 8                                         </span>
            <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">print</span><span class="token punctuation">(</span>arr<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">+</span> <span class="token string">" "</span><span class="token punctuation">)</span><span class="token punctuation">;</span>                                                           
        <span class="token punctuation">&#125;</span>                                                                                             
    <span class="token punctuation">&#125;</span>                                                                                                 
<span class="token punctuation">&#125;</span>                                                                                                     <span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
</li>
<li><p>题型: <strong>topK</strong></p>
</li>
</ul>
</li>
<li><p>排序总结:</p>
<ul>
<li><p>适用于<strong>链表排序</strong>的: 直接插入排序(O(n^2)) 归并排序(O(nlogn))</p>
</li>
<li><p>时间复杂度划分:<br>O(n^2) : 插入排序,冒泡排序,选择排序,快速排序(最坏情况),<br><strong>O(nlogn): 快速排序,堆排序,归并排序</strong></p>
</li>
</ul>
</li>
</ul>
<h2 id="方法论的优化方向："><a href="#方法论的优化方向：" class="headerlink" title="方法论的优化方向："></a>方法论的优化方向：</h2>
                
            </div>
            <hr/>

            

    <div class="reprint" id="reprint-statement">
        
            <div class="reprint__author">
                <span class="reprint-meta" style="font-weight: bold;">
                    <i class="fas fa-user">
                        文章作者:
                    </i>
                </span>
                <span class="reprint-info">
                    <a href="/myblog/about" rel="external nofollow noreferrer">Lin MingMing</a>
                </span>
            </div>
            <div class="reprint__type">
                <span class="reprint-meta" style="font-weight: bold;">
                    <i class="fas fa-link">
                        文章链接:
                    </i>
                </span>
                <span class="reprint-info">
                    <a href="https://mlming.gitee.io/myblog/myblog/2022/06/04/zuo-ti-xin-de-fang-fa-lun-shi-da-pai-xu-2/">https://mlming.gitee.io/myblog/myblog/2022/06/04/zuo-ti-xin-de-fang-fa-lun-shi-da-pai-xu-2/</a>
                </span>
            </div>
            <div class="reprint__notice">
                <span class="reprint-meta" style="font-weight: bold;">
                    <i class="fas fa-copyright">
                        版权声明:
                    </i>
                </span>
                <span class="reprint-info">
                    本博客所有文章除特別声明外，均采用
                    <a href="https://creativecommons.org/licenses/by/4.0/deed.zh" rel="external nofollow noreferrer" target="_blank">CC BY 4.0</a>
                    许可协议。转载请注明来源
                    <a href="/myblog/about" target="_blank">Lin MingMing</a>
                    !
                </span>
            </div>
        
    </div>

    <script async defer>
      document.addEventListener("copy", function (e) {
        let toastHTML = '<span>复制成功，请遵循本文的转载规则</span><button class="btn-flat toast-action" onclick="navToReprintStatement()" style="font-size: smaller">查看</a>';
        M.toast({html: toastHTML})
      });

      function navToReprintStatement() {
        $("html, body").animate({scrollTop: $("#reprint-statement").offset().top - 80}, 800);
      }
    </script>



            <div class="tag_share" style="display: block;">
                <div class="post-meta__tag-list" style="display: inline-block;">
                    
                        <div class="article-tag">
                            
                                <a href="/myblog/tags/%E7%AE%97%E6%B3%95/">
                                    <span class="chip bg-color">算法</span>
                                </a>
                            
                                <a href="/myblog/tags/%E6%8E%92%E5%BA%8F%E7%AE%97%E6%B3%95/">
                                    <span class="chip bg-color">排序算法</span>
                                </a>
                            
                        </div>
                    
                </div>
                <div class="post_share" style="zoom: 80%; width: fit-content; display: inline-block; float: right; margin: -0.15rem 0;">
                    <link rel="stylesheet" type="text/css" href="/myblog/libs/share/css/share.min.css">
<div id="article-share">

    
    <div class="social-share" data-sites="twitter,facebook,google,qq,qzone,wechat,weibo,douban,linkedin" data-wechat-qrcode-helper="<p>微信扫一扫即可分享！</p>"></div>
    <script src="/myblog/libs/share/js/social-share.min.js"></script>
    

    

</div>

                </div>
            </div>
            
        </div>
    </div>

    

    

    

    

    

    

    

    

    

    

<article id="prenext-posts" class="prev-next articles">
    <div class="row article-row">
        
        <div class="article col s12 m6" data-aos="fade-up">
            <div class="article-badge left-badge text-color">
                <i class="fas fa-chevron-left"></i>&nbsp;上一篇</div>
            <div class="card">
                <a href="/myblog/2022/06/04/zuo-ti-xin-de-fang-fa-lun-shu-zu-cao-zuo-3/">
                    <div class="card-image">
                        
                        
                        <img src="/myblog/medias/featureimages/7.jpg" class="responsive-img" alt="做题心得-方法论-数组操作(3)">
                        
                        <span class="card-title">做题心得-方法论-数组操作(3)</span>
                    </div>
                </a>
                <div class="card-content article-content">
                    <div class="summary block-with-text">
                        
                            
                        
                    </div>
                    <div class="publish-info">
                        <span class="publish-date">
                            <i class="far fa-clock fa-fw icon-date"></i>2022-06-04
                        </span>
                        <span class="publish-author">
                            
                            <i class="fas fa-bookmark fa-fw icon-category"></i>
                            
                            <a href="/myblog/categories/%E5%81%9A%E9%A2%98%E5%BF%83%E5%BE%97%E4%B8%93%E6%A0%8F/" class="post-category">
                                    做题心得专栏
                                </a>
                            
                            
                        </span>
                    </div>
                </div>
                
                <div class="card-action article-tags">
                    
                    <a href="/myblog/tags/%E7%AE%97%E6%B3%95/">
                        <span class="chip bg-color">算法</span>
                    </a>
                    
                    <a href="/myblog/tags/%E6%95%B0%E7%BB%84/">
                        <span class="chip bg-color">数组</span>
                    </a>
                    
                </div>
                
            </div>
        </div>
        
        
        <div class="article col s12 m6" data-aos="fade-up">
            <div class="article-badge right-badge text-color">
                下一篇&nbsp;<i class="fas fa-chevron-right"></i>
            </div>
            <div class="card">
                <a href="/myblog/2022/06/04/simplespring-kai-fa-ye-wu-xi-tong-de-gou-jian-zi-yan-kuang-jia-de-qi-yuan-2/">
                    <div class="card-image">
                        
                        
                        <img src="/myblog/medias/featureimages/20.jpg" class="responsive-img" alt="SimpleSpring开发-业务系统的构建[自研框架的起源](2)">
                        
                        <span class="card-title">SimpleSpring开发-业务系统的构建[自研框架的起源](2)</span>
                    </div>
                </a>
                <div class="card-content article-content">
                    <div class="summary block-with-text">
                        
                            
                        
                    </div>
                    <div class="publish-info">
                            <span class="publish-date">
                                <i class="far fa-clock fa-fw icon-date"></i>2022-06-04
                            </span>
                        <span class="publish-author">
                            
                            <i class="fas fa-bookmark fa-fw icon-category"></i>
                            
                            <a href="/myblog/categories/SimpleSpring%E5%BC%80%E5%8F%91%E4%B8%93%E6%A0%8F/" class="post-category">
                                    SimpleSpring开发专栏
                                </a>
                            
                            
                        </span>
                    </div>
                </div>
                
                <div class="card-action article-tags">
                    
                    <a href="/myblog/tags/%E5%90%8E%E7%AB%AF/">
                        <span class="chip bg-color">后端</span>
                    </a>
                    
                    <a href="/myblog/tags/%E9%80%A0%E8%BD%AE%E5%AD%90/">
                        <span class="chip bg-color">造轮子</span>
                    </a>
                    
                    <a href="/myblog/tags/Spring/">
                        <span class="chip bg-color">Spring</span>
                    </a>
                    
                </div>
                
            </div>
        </div>
        
    </div>
</article>

</div>



<!-- 代码块功能依赖 -->
<script type="text/javascript" src="/myblog/libs/codeBlock/codeBlockFuction.js"></script>


  <!-- 是否加载使用自带的 prismjs. -->
  <script type="text/javascript" src="/myblog/libs/prism/prism.min.js"></script>


<!-- 代码语言 -->

<script type="text/javascript" src="/myblog/libs/codeBlock/codeLang.js"></script>


<!-- 代码块复制 -->

<script type="text/javascript" src="/myblog/libs/codeBlock/codeCopy.js"></script>


<!-- 代码块收缩 -->

<script type="text/javascript" src="/myblog/libs/codeBlock/codeShrink.js"></script>



    </div>
    <div id="toc-aside" class="expanded col l3 hide-on-med-and-down">
        <div class="toc-widget card" style="background-color: white;">
            <div class="toc-title"><i class="far fa-list-alt"></i>&nbsp;&nbsp;目录</div>
            <div id="toc-content"></div>
        </div>
    </div>
</div>

<!-- TOC 悬浮按钮. -->

<div id="floating-toc-btn" class="hide-on-med-and-down">
    <a class="btn-floating btn-large bg-color">
        <i class="fas fa-list-ul"></i>
    </a>
</div>


<script src="/myblog/libs/tocbot/tocbot.min.js"></script>
<script>
    $(function () {
        tocbot.init({
            tocSelector: '#toc-content',
            contentSelector: '#articleContent',
            headingsOffset: -($(window).height() * 0.4 - 45),
            collapseDepth: Number('0'),
            headingSelector: 'h2, h3, h4'
        });

        // Set scroll toc fixed.
        let tocHeight = parseInt($(window).height() * 0.4 - 64);
        let $tocWidget = $('.toc-widget');
        $(window).scroll(function () {
            let scroll = $(window).scrollTop();
            /* add post toc fixed. */
            if (scroll > tocHeight) {
                $tocWidget.addClass('toc-fixed');
            } else {
                $tocWidget.removeClass('toc-fixed');
            }
        });

        
        /* 修复文章卡片 div 的宽度. */
        let fixPostCardWidth = function (srcId, targetId) {
            let srcDiv = $('#' + srcId);
            if (srcDiv.length === 0) {
                return;
            }

            let w = srcDiv.width();
            if (w >= 450) {
                w = w + 21;
            } else if (w >= 350 && w < 450) {
                w = w + 18;
            } else if (w >= 300 && w < 350) {
                w = w + 16;
            } else {
                w = w + 14;
            }
            $('#' + targetId).width(w);
        };

        // 切换TOC目录展开收缩的相关操作.
        const expandedClass = 'expanded';
        let $tocAside = $('#toc-aside');
        let $mainContent = $('#main-content');
        $('#floating-toc-btn .btn-floating').click(function () {
            if ($tocAside.hasClass(expandedClass)) {
                $tocAside.removeClass(expandedClass).hide();
                $mainContent.removeClass('l9');
            } else {
                $tocAside.addClass(expandedClass).show();
                $mainContent.addClass('l9');
            }
            fixPostCardWidth('artDetail', 'prenext-posts');
        });
        
    });
</script>

    

</main>




    <footer class="page-footer bg-color">
    

    <div class="container row center-align"
         style="margin-bottom: 0px !important;">
        <div class="col s12 m8 l8 copy-right">
            Copyright&nbsp;&copy;
            
                <span id="year">2022</span>
            
            <a href="/myblog/about" target="_blank">Lin MingMing</a>
            |&nbsp;Powered by&nbsp;<a href="https://hexo.io/" target="_blank">Hexo</a>
            |&nbsp;Theme&nbsp;<a href="https://github.com/blinkfox/hexo-theme-matery" target="_blank">Matery</a>
            <br>
            
                &nbsp;<i class="fas fa-chart-area"></i>&nbsp;站点总字数:&nbsp;<span
                        class="white-color">31.5k</span>
            
            
            
                
            
            
                <span id="busuanzi_container_site_pv">
                &nbsp;|&nbsp;<i class="far fa-eye"></i>&nbsp;总访问量:&nbsp;
                    <span id="busuanzi_value_site_pv" class="white-color"></span>
            </span>
            
            
                <span id="busuanzi_container_site_uv">
                &nbsp;|&nbsp;<i class="fas fa-users"></i>&nbsp;总访问人数:&nbsp;
                    <span id="busuanzi_value_site_uv" class="white-color"></span>
            </span>
            
            <br>

            <!-- 运行天数提醒. -->
            
            <br>
            
        </div>
        <div class="col s12 m4 l4 social-link social-statis">
    <a href="https://gitee.com/mmwindy" class="tooltipped" target="_blank" data-tooltip="访问我的GitHub" data-position="top" data-delay="50">
        <i class="fab fa-github"></i>
    </a>



    <a href="mailto:1139380762@qq.com" class="tooltipped" target="_blank" data-tooltip="邮件联系我" data-position="top" data-delay="50">
        <i class="fas fa-envelope-open"></i>
    </a>







    <a href="tencent://AddContact/?fromId=50&fromSubId=1&subcmd=all&uin=1139380762" class="tooltipped" target="_blank" data-tooltip="QQ联系我: 1139380762" data-position="top" data-delay="50">
        <i class="fab fa-qq"></i>
    </a>







</div>
    </div>
</footer>

<div class="progress-bar"></div>


    <!-- 搜索遮罩框 -->
<div id="searchModal" class="modal">
    <div class="modal-content">
        <div class="search-header">
            <span class="title"><i class="fas fa-search"></i>&nbsp;&nbsp;搜索</span>
            <input type="search" id="searchInput" name="s" placeholder="请输入搜索的关键字"
                   class="search-input">
        </div>
        <div id="searchResult"></div>
    </div>
</div>

<script type="text/javascript">
$(function () {
    var searchFunc = function (path, search_id, content_id) {
        'use strict';
        $.ajax({
            url: path,
            dataType: "xml",
            success: function (xmlResponse) {
                // get the contents from search data
                var datas = $("entry", xmlResponse).map(function () {
                    return {
                        title: $("title", this).text(),
                        content: $("content", this).text(),
                        url: $("url", this).text()
                    };
                }).get();
                var $input = document.getElementById(search_id);
                var $resultContent = document.getElementById(content_id);
                $input.addEventListener('input', function () {
                    var str = '<ul class=\"search-result-list\">';
                    var keywords = this.value.trim().toLowerCase().split(/[\s\-]+/);
                    $resultContent.innerHTML = "";
                    if (this.value.trim().length <= 0) {
                        return;
                    }
                    // perform local searching
                    datas.forEach(function (data) {
                        var isMatch = true;
                        var data_title = data.title.trim().toLowerCase();
                        var data_content = data.content.trim().replace(/<[^>]+>/g, "").toLowerCase();
                        var data_url = data.url;
                        data_url = data_url.indexOf('/') === 0 ? data.url : '/' + data_url;
                        var index_title = -1;
                        var index_content = -1;
                        var first_occur = -1;
                        // only match artiles with not empty titles and contents
                        if (data_title !== '' && data_content !== '') {
                            keywords.forEach(function (keyword, i) {
                                index_title = data_title.indexOf(keyword);
                                index_content = data_content.indexOf(keyword);
                                if (index_title < 0 && index_content < 0) {
                                    isMatch = false;
                                } else {
                                    if (index_content < 0) {
                                        index_content = 0;
                                    }
                                    if (i === 0) {
                                        first_occur = index_content;
                                    }
                                }
                            });
                        }
                        // show search results
                        if (isMatch) {
                            str += "<li><a href='" + data_url + "' class='search-result-title'>" + data_title + "</a>";
                            var content = data.content.trim().replace(/<[^>]+>/g, "");
                            if (first_occur >= 0) {
                                // cut out 100 characters
                                var start = first_occur - 20;
                                var end = first_occur + 80;
                                if (start < 0) {
                                    start = 0;
                                }
                                if (start === 0) {
                                    end = 100;
                                }
                                if (end > content.length) {
                                    end = content.length;
                                }
                                var match_content = content.substr(start, end);
                                // highlight all keywords
                                keywords.forEach(function (keyword) {
                                    var regS = new RegExp(keyword, "gi");
                                    match_content = match_content.replace(regS, "<em class=\"search-keyword\">" + keyword + "</em>");
                                });

                                str += "<p class=\"search-result\">" + match_content + "...</p>"
                            }
                            str += "</li>";
                        }
                    });
                    str += "</ul>";
                    $resultContent.innerHTML = str;
                });
            }
        });
    };

    searchFunc('/myblog/search.xml', 'searchInput', 'searchResult');
});
</script>

    <!-- 回到顶部按钮 -->
<div id="backTop" class="top-scroll">
    <a class="btn-floating btn-large waves-effect waves-light" href="#!">
        <i class="fas fa-arrow-up"></i>
    </a>
</div>


    <script src="/myblog/libs/materialize/materialize.min.js"></script>
    <script src="/myblog/libs/masonry/masonry.pkgd.min.js"></script>
    <script src="/myblog/libs/aos/aos.js"></script>
    <script src="/myblog/libs/scrollprogress/scrollProgress.min.js"></script>
    <script src="/myblog/libs/lightGallery/js/lightgallery-all.min.js"></script>
    <script src="/myblog/js/matery.js"></script>

    

    

    <!-- 雪花特效 -->
    

    <!-- 鼠标星星特效 -->
     
        <script type="text/javascript">
            // 只在桌面版网页启用特效
            var windowWidth = $(window).width();
            if (windowWidth > 768) {
                document.write('<script type="text/javascript" src="/myblog/libs/others/star.js"><\/script>');
            }
        </script>
    

    

    <!-- Baidu Analytics -->

    <!-- Baidu Push -->

<script>
    (function () {
        var bp = document.createElement('script');
        var curProtocol = window.location.protocol.split(':')[0];
        if (curProtocol === 'https') {
            bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';
        } else {
            bp.src = 'http://push.zhanzhang.baidu.com/push.js';
        }
        var s = document.getElementsByTagName("script")[0];
        s.parentNode.insertBefore(bp, s);
    })();
</script>

    
    <script src="/myblog/libs/others/clicklove.js" async="async"></script>
    
    
    <script async src="/myblog/libs/others/busuanzi.pure.mini.js"></script>
    

    

    

    <!--腾讯兔小巢-->
    
    

    

    

    
    <script src="/myblog/libs/instantpage/instantpage.js" type="module"></script>
    

</body>

</html>
